package com.hongxun.design.utils.dragutil;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

import com.hongxun.design.bean.PhotoPassagewayItemBean;
import com.hongxun.design.mytools.MyLog;

/**
 * Created by zerdoor_pc on 2016/5/17.
 */
public class DragWithUtil {
    Context context;
    View view;

    private int screenWidth;
    int[] line;
    RelativeLayout container;
    PhotoPassagewayItemBean itemBean;
    int math = 0;
    int max = 0;
    RelativeLayout.LayoutParams params;
    Handler xyhandler;

    @SuppressLint("NewApi")
    public DragWithUtil(Context context, View view, int[] line, PhotoPassagewayItemBean itemBean, RelativeLayout container, RelativeLayout.LayoutParams l, Handler xyhandler) {
        this.context = context;
        this.view = view;
        this.line = line;
        this.itemBean = itemBean;
        this.container = container;
        this.params = l;
        this.xyhandler = xyhandler;
        view.setClickable(true);
        view.setOnTouchListener(touchLisener);
        int[] s = new int[2];
        container.getLocationOnScreen(s);

        int[] s2 = new int[2];
        view.getLocationOnScreen(s2);

        math = (int) (-s2[0] + container.getX());
        max = (int) (container.getX() + container.getWidth() - s2[0] - view.getWidth());
    }

    long currtime = 0;
    View.OnTouchListener touchLisener = new View.OnTouchListener() {
        int lastX, lastY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    currtime = System.currentTimeMillis();
                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = (int) event.getRawX() - lastX;
//                  int dy = (int) event.getRawY() - lastY;
                    MyLog.L("getRawXgetRawX=-------------------------------------------------------------------");
                    MyLog.L("getRawXgetRawX=lastX=" + lastX);
                    MyLog.L("getRawXgetRawX=event.getRawX()=" + event.getRawX());
                    MyLog.L("getRawXgetRawX=dx=" + dx);
                    MyLog.L("getRawXgetRawX=v.getLeft()=" + v.getLeft());
                    int dy = 0;

                    int left = v.getLeft() + dx;
                    int top = v.getTop() + dy;
                    int right = v.getRight() + dx;
                    int bottom = v.getBottom() + dy;

                    if (left < math) {

                        left = math;
                        right = left + v.getWidth();
                    }

                    if (left > max) {
                        left = max;
                        right = left + v.getWidth();
                    }
                    if (right > container.getMeasuredWidth()) {
                        right = container.getMeasuredWidth();
                        left = right - v.getWidth();
                    }


                    if (top < 0) {
                        top = 0;
                        bottom = top + v.getHeight();
                    }

                    if (bottom > container.getMeasuredHeight()) {
                        bottom = container.getMeasuredHeight();
                        top = bottom - v.getHeight();
                    }

                    v.layout(left, top, right, bottom);

                    lastX = (int) event.getRawX();
                    lastY = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_UP:
                    params.setMargins(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
                    int[] s2 = new int[2];
                    v.getLocationOnScreen(s2);
                    s2[0]=s2[0]-v.getWidth()*3/2;
                    itemBean.setXy(s2);
                    xyhandler.sendEmptyMessage(1);
                    MyLog.L("currtimecurrtimecurrtime:System.currentTimeMillis()=" + (System.currentTimeMillis() ));
                    MyLog.L("currtimecurrtimecurrtime:currtime=" + (currtime ));
                    MyLog.L("currtimecurrtimecurrtime=" + (System.currentTimeMillis()-currtime));
                    if (System.currentTimeMillis()-currtime>150){
                        return true;
                    }else{
                        break;
                    }

                default:
                    break;
            }
            return false;
        }
    };

    private void setBestPosition(View v) {
        int width = v.getWidth();
        int left = v.getLeft();
        int selectedPosition = Math.round(1.0F * left / width);//四舍五入
        int toPosition = selectedPosition * width;
        v.layout(selectedPosition * width, v.getTop(),
                selectedPosition * width + width, v.getBottom());
        TranslateAnimation animation = new TranslateAnimation(left - toPosition, 0, 0, 0);
        animation.setInterpolator(new LinearInterpolator());
        animation.setDuration(400);
        animation.setFillAfter(true);
        v.startAnimation(animation);
//        v.invalidate();
    }
}
